﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZSZ.AdminWeb.App_Start;
using ZSZ.AdminWeb.Models.Role;
using ZSZ.CommonMvc;
using ZSZ.Iservice;

namespace ZSZ.AdminWeb.Controllers
{
    public class RoleController : Controller
    {
        public IRoleService RoleService { get; set; }
        public IPermissionService PermissionService { get; set; }
        [CheckPermission("Role.List")]
        // GET: Role
        public ActionResult List()
        {
            var roles = RoleService.GetAll();
            return View(roles);
        }
        [HttpPost]
        [CheckPermission("Role.Delete")]
        public ActionResult Delete(long id) {
            RoleService.MarkDeleted(id);
            return Json(new JsonCommon { Status = "ok" });
        }
        [HttpGet]
        [CheckPermission("Role.Add")]
        public ActionResult Add() {
            var persss = PermissionService.GetAll();
            return View(persss);
        }
        [HttpPost]
        [CheckPermission("Role.Add")]
        public ActionResult Add(string name,long[] PermissionIds) {
            long id =  RoleService.AddNew(name);
            PermissionService.AddPermIds(id, PermissionIds);
            return Json(new JsonCommon { Status="ok"});
        }
        [HttpPost]
        [CheckPermission("Role.Delete")]
        public ActionResult PiLiangShan(long[] ids) {
            foreach (var item in ids)
            {
                RoleService.MarkDeleted(item);
            }
            return Json(new JsonCommon { Status="ok"});
        }
        [HttpGet]
        [CheckPermission("Role.Edit")]
        public ActionResult Edit(long id) {
            var roledto = RoleService.GetById(id);
            var pers = PermissionService.GetAll();
            var check = PermissionService.GetByRoleId(roledto.Id);
            RoleEditGet model = new RoleEditGet();
            model.Role = roledto;
            model.QuanPer = pers;
            model.CheckPer = check;
            return View(model);
        }
        [HttpPost]
        [CheckPermission("Role.Edit")]
        public ActionResult Edit(long id,string name, long[] PermissionIds) {
            RoleService.Update(id,name);
            PermissionService.UpdatePermIds(id, PermissionIds);
            return Json(new JsonCommon { Status="ok"});
        }
    }
}